package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.TeacherCourseDto;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {

    @Autowired
    private CourseTeacherMapper courseTeacherMapper;


    @Override
    public List<CourseTeacher> teacherList(Long id) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, id);
        return courseTeacherMapper.selectList(queryWrapper);
    }


    @Override
    public CourseTeacher addOrUpdateTeacher(CourseTeacher courseTeacher) {

        if (courseTeacher.getId()==null){
            // 封装数据
            CourseTeacher teacher=new CourseTeacher();
            // 属性拷贝
            BeanUtils.copyProperties(courseTeacher,teacher);
            // 添加时间
            courseTeacher.setCreateDate(LocalDateTime.now());
            // 获取课程id
            Long courseId = courseTeacher.getCourseId();
            // 转化long 设置课程id
            courseTeacher.setCourseId(courseId);

            int insert = courseTeacherMapper.insert(courseTeacher);
            if(insert<=0){
                XueChengPlusException.cast("添加课程老师信息失败");
            }

            //查詢添加的课程老师信息
            CourseTeacher courseTeacher1 = courseTeacherMapper.selectById(courseTeacher.getId());

            return courseTeacher1;
        }else {

            Long id = courseTeacher.getId();

            CourseTeacher teacher = courseTeacherMapper.selectById(id);

            if (teacher==null){
                XueChengPlusException.cast("课程老师信息不存在");
            }

            courseTeacher.setCreateDate(teacher.getCreateDate());
            courseTeacher.setPhotograph(teacher.getPhotograph());

            int update = courseTeacherMapper.updateById(courseTeacher);

            if(update<=0){
                XueChengPlusException.cast("更新课程老师信息失败");
            }

            //查询更新后的课程老师信息
            CourseTeacher courseTeacher1 = courseTeacherMapper.selectById(courseTeacher.getId());
            return courseTeacher1;

        }


    }

    @Override
    public void deleteTeacher(Long courseId, Long teacherId) {

        LambdaQueryWrapper<CourseTeacher> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, courseId);
        queryWrapper.eq(CourseTeacher::getId, teacherId);

        int delete = courseTeacherMapper.delete(queryWrapper);

        if (delete<=0){
            XueChengPlusException.cast("删除课程老师信息失败");
        }

    }


}
